############################################################
# Outcomes BEPS 2010 traditional and design-based approach
############################################################

rm (list = ls())

library(lmtest)
library(sandwich)
library(stargazer)

# load function that does clustered SEs
vcovCluster <- function(
  model,
  cluster
)
{
  require(sandwich)
  require(lmtest)
  if(nrow(model.matrix(model))!=length(cluster)){
    stop("check your data: cluster variable has different N than model")
  }
  M <- length(unique(cluster))
  N <- length(cluster)           
  K <- model$rank   
  if(M<40){
    warning("Fewer than 40 clusters, variances may be unreliable")
  }
  dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
  uj  <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
  rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
  return(rcse.cov)
}

#########################
# Load and explore data
#########################

# Generate log  
#sink("011_outcomes_beps_2010.txt")

# Read data 
load("011_panel_brazil_beps2010_traditional_socio_2017jan11.RData")
d_traditional =  d
d = NULL

# Read data
load("012_panel_brazil_beps2010_db_socio_2017jan11.RData")

#################################
# Results design based approach
#################################

mod_db = lm(d_match$outcome  ~ d_match$t_ind + 
                               d_match$vote_incumbent + 
                               d_match$ideology_imp + 
                               d_match$partyid_pt +
                               as.factor(d_match$sector))
mod_db_c <- coeftest(mod_db, vcov = vcovCluster(mod_db, cluster = d_match$sector))

#################################
# Results traditional
#################################

mod_tra = lm(d_traditional$outcome  ~ d_traditional$t_ind + 
                                      d_traditional$vote_incumbent + 
                                      d_traditional$ideology_imp + 
                                      d_traditional$partyid_pt + 
                                      as.factor(d_traditional$brasec))
mod_tra_c <- coeftest(mod_tra, vcov = vcovCluster(mod_tra, cluster = d_traditional$brasec))

##############
# Table LaTeX
##############

# TABLE 1 APPENDIX 
dim(d_traditional)
dim(d_match)
stargazer(mod_tra_c,mod_db_c,
          type = "text",
          title="Regression results sociotropic treatment",
          align=TRUE, 
          omit.stat=c("LL","ser","f","rsq","adj.rsq"), 
          no.space=TRUE,  
          multicolumn = TRUE,
          table.placement = "H",
          covariate.labels=c("Sociotropic perceptions (1-5)","Negative sociotropic treatment"),
          dep.var.caption  = "Voting for the incumbent",
          dep.var.labels.include = FALSE,
          omit = c("vote_incumbent", 
                   "ideology", 
                   "thermpres", 
                   "ideology", 
                   "partyid_pt",
                   "sector", 
                   "brasec",
                   "Constant"),
          add.lines = list(c("Controls","Yes","Yes"),
                           c("Neighborhood Fixed effects","Yes","Yes"),
                           c("Observations","896","42")))

#sink()
